package com.example.xyr.myapplication.adapter

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.AbsListView.LayoutParams
import android.widget.BaseAdapter
import android.widget.ImageView
import com.example.xyr.myapplication.R
import com.example.xyr.myapplication.utils.EmotionUtils
import com.example.xyr.myapplication.utils.LogUtil

/**
 * Created by ylcf on 2018/9/6.
 */
class EmotionGridViewAdapter : BaseAdapter {

    private lateinit var context: Context
    private var emotionNames: List<String> = arrayListOf()
    private var itemWidth: Int = 0
    private var emotion_map_type: Int = 0

    constructor(context: Context, emotionNames: List<String>, itemWidth: Int, emotion_map_type: Int) : super() {
        this.context = context
        this.emotionNames = emotionNames
        this.itemWidth = itemWidth
        this.emotion_map_type = emotion_map_type
    }


    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        val iv_emotion = ImageView(context)
        // 设置内边距
        iv_emotion.setPadding(itemWidth / 8, itemWidth / 8, itemWidth / 8, itemWidth / 8)
        val params = LayoutParams(itemWidth, itemWidth)
        iv_emotion.layoutParams = params

        //判断是否为最后一个item
        if (position == count - 1) {
            iv_emotion.setImageResource(R.drawable.compose_emotion_delete)
        } else {
            val emotionName = emotionNames[position]
            LogUtil.e("tag","表情》》》》》"+emotionName)
            iv_emotion.setImageResource(EmotionUtils.getImgByName(emotion_map_type, emotionName))
        }

        return iv_emotion
    }

    override fun getItem(position: Int): Any {
        return emotionNames[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getCount(): Int {
        // +1 最后一个为删除按钮
        return emotionNames.size + 1
    }
}